package com.licq.ldoc.domain;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.licq.ldoc.writer.visitor.Visitor;
import com.lowagie.text.BadElementException;

public class Element extends AbstractNode {
    private static final String LINK_PREFIX = "#";
	private int level;
	private ElementType type;
    private String anchor;
    private List<Attribute> attributes = new ArrayList<Attribute>();
    private List<AttributeElement> attributeElements = new ArrayList<AttributeElement>();

    public int getLevel() {
        return level;
    }

    public List<Attribute> getAttributes() {
        return attributes;
    }

    public Element(int level, String name, ElementType type, Occurrencies occurrences) {
        this(level, name, type, occurrences, false);
    }
    
    public Element(int level, String name, ElementType type, Occurrencies occurrences, boolean required) {
    	this.level = level;
    	this.name = name;
    	this.type = type;
    	this.occurrences = occurrences;
    	this.required = required;
	}

    public ElementType getType() {
		return type;
	}

	public void setAttributes(List<Attribute> attributes) {
        this.attributes = attributes;
    }

    public void addAttribute(Attribute attribute) {
        attributes.add(attribute);
    }

    public void addAttributeElement(AttributeElement subElement) {
        attributeElements.add(subElement);
    }

    public List<AttributeElement> getAttributeElements() {
        return attributeElements;
    }

    public void accept(Visitor visitor) throws BadElementException {
        visitor.visit(this);
    }

	public String getAnchor() {
		return anchor;
	}

	public void setAnchor(String anchor) {
		this.anchor = anchor;
	}
	
	public void makeAnchor() {
		setAnchor(name + System.currentTimeMillis());
	}
	
	public boolean hasAnchor() {
		return StringUtils.isNotBlank(anchor) 
			&& !anchor.startsWith(LINK_PREFIX);
	}
	
	public void setLink(String link) {
		this.anchor = link.startsWith(LINK_PREFIX) ? link : LINK_PREFIX + link;
	}
	
	public boolean hasLink() {
		return StringUtils.isNotBlank(anchor) 
			&& anchor.startsWith(LINK_PREFIX);
	}
	
}
